﻿using KhtApi.BLL;
using KhtApi.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KhtMaintain
{
    public partial class FrmBomFix : Form
    {
        KhtBLL bll = null;
        List<Bom> boms = null;
        int currIndex = 0;
        public FrmBomFix(KhtBLL bll)
        {
            this.bll = bll;
            InitializeComponent();
        }

        private void FrmBomFix_Load(object sender, EventArgs e)
        {

        }

        private void btnGetAllBom_Click(object sender, EventArgs e)
        {
            staInfo.Text = "正在读取BOM单，请稍后...";
            Application.DoEvents();
            boms= bll.GetBoms();
            if (boms.Count > 0)
            {
                ShowBom(1);
                currIndex = 1;
            }
            staInfo.Text = "完成读取BOM单";
        }

        private void ShowBom(int n)
        {
            txtProgress.Text = n.ToString() + "/" + boms.Count;
            n--;
            txtBomId.Text = boms[n].BomId;
            txtCat1.Text = boms[n].Catagory1;
            txtCat2.Text = boms[n].Catagory2;
            txtCat3.Text = boms[n].Catagory3;
            txtItemCode.Text = boms[n].ItemCode;
            txtName.Text = boms[n].Name;
            txtModelNo.Text = boms[n].ModelNo;
            txtOringinal.Text = boms[n].Oringinal;
            txtSpec.Text = boms[n].Spec;
            txtVersion.Text = boms[n].Version;
            dgvData.DataSource = null;
            dgvData.DataSource = boms[n].Materials;
            
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            currIndex--;
            if (currIndex < 1) currIndex = boms.Count;
            ShowBom(currIndex);
                
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            currIndex++;
            if (currIndex > boms.Count) currIndex = 1;
            ShowBom(currIndex);
        }

        private void btnFixThis_Click(object sender, EventArgs e)
        {
            bll.FixBomProduct(boms[currIndex]);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            boms= bll.GetBoms(txtPrdCodeSearch.Text);
            if (boms.Count > 0) ShowBom(1);
        }

        private void btnBatchFix_Click(object sender, EventArgs e)
        {
            bll.FixBomMaterials(boms[currIndex]);
        }
    }
}
